package com.ttg.controller;

//~--- non-JDK imports --------------------------------------------------------

import com.ttg.config.Properties;
import com.ttg.contants.KeyContants;
import com.ttg.data.InitGatewayReqData;
import com.ttg.pojo.PaymentReqDTO;
import com.ttg.security.SecurityKit;
import com.ttg.utils.HttpKit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

//~--- classes ----------------------------------------------------------------

/**
 * Class GwPayDemoController
 * Description 说明：支付接口测试DEMO
 * Create 2016-08-09 13:38:43
 * @author Ardy
 */
@Controller
@RequestMapping
public class GwPayDemoController {

    /**
     * Field LOGGER
     * Description
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(GwPayDemoController.class);

    /**
     * Method testPayDemo
     * Description 说明：
     *
     * @return 返回值说明：
     */
    @RequestMapping("/pay")
    public ModelAndView testPayDemo() {
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName("demo/pay_req_demo");

        PaymentReqDTO paymentReq = InitGatewayReqData.initPaymentReq();
        String        url        = "req_pay";

        LOGGER.debug("请求地址：{}", url);
        modelAndView.addObject("url", url);
        modelAndView.addObject("payment", paymentReq);

        return modelAndView;
    }

    /**
     * Method sendPayReq
     * Description 说明：
     *
     * @param merchantId 说明：
     * @param merOrderId 说明：
     * @param userId 说明：
     * @param feeAmt 说明：
     * @param currency 说明：
     * @param subject 说明：
     * @param detail 说明：
     * @param transAmt 说明：
     * @param bgRetUrl 说明：
     * @param pgRetUrl 说明：
     * @param mediPwdCheck 说明：
     * @param attach 说明：
     * @param mediPwd 说明：
     * @param immediatelyPay 说明：
     *
     * @return 返回值说明：
     */
    @RequestMapping(
        value    = "/req_pay",
        method   = RequestMethod.POST
    )
    public ModelAndView sendPayReq(@RequestParam(
        value    = "merchantId",
        required = true
    ) String merchantId, @RequestParam(
        value    = "merOrderId",
        required = true
    ) String merOrderId, @RequestParam(
        value    = "userId",
        required = true
    ) String userId, @RequestParam(
        value    = "feeAmt",
        required = true
    ) Integer feeAmt, @RequestParam(
        value    = "currency",
        required = true
    ) String currency, @RequestParam(
        value    = "subject",
        required = true
    ) String subject, @RequestParam(
        value    = "detail",
        required = false
    ) String detail, @RequestParam(
        value    = "transAmt",
        required = true
    ) Integer transAmt, @RequestParam(
        value    = "bgRetUrl",
        required = false
    ) String bgRetUrl, @RequestParam(
        value    = "pgRetUrl",
        required = false
    ) String pgRetUrl, @RequestParam(
        value    = "mediPwdCheck",
        required = true
    ) Integer mediPwdCheck, @RequestParam(
        value    = "attach",
        required = false
    ) String attach, @RequestParam(
        value    = "mediPwd",
        required = false
    ) String mediPwd, @RequestParam(
        value    = "immediatelyPay",
        required = true
    ) Integer immediatelyPay) {
        PaymentReqDTO paymentReqDTO = new PaymentReqDTO();

        paymentReqDTO.setMerchantId(merchantId);
        paymentReqDTO.setMerOrderId(merOrderId);
        paymentReqDTO.setUserId(userId);
        paymentReqDTO.setFeeAmt(feeAmt);
        paymentReqDTO.setCurrency(currency);
        paymentReqDTO.setSubject(subject);
        paymentReqDTO.setDetail(detail);
        paymentReqDTO.setTransAmt(transAmt);
        paymentReqDTO.setBgRetUrl(bgRetUrl);
        paymentReqDTO.setPgRetUrl(pgRetUrl);
        paymentReqDTO.setMediPwdCheck(mediPwdCheck);
        paymentReqDTO.setMediPwd(mediPwd);
        paymentReqDTO.setAttach(attach);
        paymentReqDTO.setImmediatelyPay(immediatelyPay);

        String url = Properties.URL.getString("GATEWAY_BASE_SCHEME") + Properties.URL.getString("PAY_URL");

        paymentReqDTO.setSignatureAlgorithm(KeyContants.SECURITY_TYPE);
        paymentReqDTO.setSignatureInfo(
            SecurityKit.signByRSA(HttpKit.httpGwParam2SignStr(paymentReqDTO.getAllParameters())));
        LOGGER.debug("开户请求参数：{},参与签名串：{}", paymentReqDTO,
                     HttpKit.httpGwParam2SignStr(paymentReqDTO.getAllParameters()));
        LOGGER.debug("请求地址：{}", url);

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName("req/pay_demo");
        modelAndView.addObject("url", url);
        modelAndView.addObject("payment", paymentReqDTO);

        return modelAndView;
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
